package com.xucy.springboot.redis.PubSub;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

/**
 * @Author xucy
 * @Date 2019-07-04 14:15
 * @Description 订阅者
 **/
@Component
public class SubController implements MessageListener {

    private Logger logger= LoggerFactory.getLogger(SubController.class);
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void onMessage(Message message, byte[] bytes) {
        RedisSerializer<String> stringSerializer =
                stringRedisTemplate.getStringSerializer();

        String deserialize = stringSerializer.deserialize(message.getBody());

        logger.info("接收到的订阅信息是："+deserialize);

    }
}
